package com.hecore.lagou.work.partition;

import com.hecore.lagou.work.entity.SortNumBean;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Partitioner;

/**
 * @Author: hecore
 * @Date: 2020/10/19 01:35
 * @Description: 自定义排序分区,由于数字有其大小,为在集群下正确使用，不造成OOM,需要控制分区范围
 * Partition Key-value同Reducer
 */

public class SortNumPartition extends Partitioner<SortNumBean, NullWritable> {

    /**
     * 重新编排分区函数
     *      采用三个分区设计
     * @param nullWritable 空实现参数,占位符
     * @param sortNumBean 自定义排序Bean
     * @param numPartitions 排序分区
     * @return
     */

    @Override
    public int getPartition(SortNumBean sortNumBean, NullWritable nullWritable, int numPartitions) {
        int keyInt = Integer.parseInt(String.valueOf(sortNumBean.getNum()));
        if (keyInt < 65224) {
            return 0;
        } else if (keyInt < 65224) {
            return 1;
        } else {
            return 2;
        }
    }
}
